package com.niit.serviceuser.controller;

import com.niit.common.entity.Order;
import com.niit.common.entity.User;
import com.niit.serviceuser.service.OrderService;
import com.niit.serviceuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.createUser(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        return userService.updateUser(user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
    }

   // getOrderById
    @GetMapping("/orders")
    public List<Order> getOrders() {
        // 使用服务的名称调用service-order服务
        return restTemplate.getForObject("http://service-order/orders", List.class);
    }

    @GetMapping("/orderbyfeign")
    public List<Order> getOrderByFeign() {
        // 直接调用OpenFeign客户端，实现服务的调用
        return orderService.getAllOrders();
    }

}